﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CLibArtreve.dal;
using System.Configuration;
using CLibArtreve.models;
using System.Data;

namespace CLibArtreve.bll
{
    public class ArtBLL
    {
        #region VARIABLES
        private string _connectionString;        
        private ArtDAL _adapter;
        #endregion

        #region PROPERTIES
        public string ConnectionString
        {
            get {
                if (_connectionString == null)
                    _connectionString = ConfigurationManager.ConnectionStrings["ARTREVEConnectionString"].ConnectionString;
                return _connectionString; 
            }
            set { _connectionString = value; }
        }
        public ArtDAL Adapter
        {
            get
            {
                if (_adapter == null)
                    _adapter = new ArtDAL(ConnectionString);
                return _adapter;
            }
        }
        #endregion

        #region CONSTRUCTOR
        public ArtBLL(string connectionString)
        {
            ConnectionString = connectionString;
        }
        #endregion

        #region OVERRIDE METHODS
        public override string ToString()
        {
            return "BUSINESS LOGIC LAYER: " + this.GetType().ToString();
        }
        #endregion

        #region GET METHODS
        public IList<Art> GetAllArts()
        {
            try
            {
                //GET DATASET
                DataSet ds = Adapter.GetAllArts();
                //MAKE A LIST
                IList<Art> list = new List<Art>();
                //LOOP THROUGH ROWS OF DATASET
                foreach (DataRow row in ds.Tables[0].Rows)
                {
                    list.Add(GenerateArtFromDataRow(row));
                }
                //RETURN LIST
                return list;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public IList<Art> GetAllArtsWithCategories()
        {
            try
            {
                //GET DATASET
                DataSet ds = Adapter.GetAllArtsWithCategories();
                //MAKE A LIST
                IList<Art> list = new List<Art>();
                //LOOP THROUGH ROWS OF DATASET
                foreach (DataRow row in ds.Tables[0].Rows)
                {
                    list.Add(GenerateArtFromDataRow(row));
                }
                //RETURN LIST
                return list;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }        
        public IList<Art> GetAllSpotlightedArts()
        {
            try
            {
                //GET DATASET
                DataSet ds = Adapter.GetAllSpotlightedArts();
                //MAKE A LIST
                IList<Art> list = new List<Art>();
                //LOOP THROUGH ROWS OF DATASET
                foreach (DataRow row in ds.Tables[0].Rows)
                {
                    list.Add(GenerateArtFromDataRow(row));
                }
                //RETURN LIST
                return list;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }        
        public Art GetArtById(Int64? id)
        {
            try
            {
                if (id > 0)
                {
                    //GET DATASET
                    DataSet ds = Adapter.GetArtById(id);
                    //IF DATASET EXISTS EN IT HAS ROWS RETURN THE ROW
                    if (ds != null && ds.Tables[0].Rows.Count > 0)
                    {
                        DataRow row = ds.Tables[0].Rows[0];
                        return GenerateArtFromDataRow(row);
                    }
                }
                return null;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public IList<Art> GetArtByUserId(Int64? id)
        {
            try
            {
                //GET DATASET
                DataSet ds = Adapter.GetArtByUserId(id);
                //MAKE A LIST
                IList<Art> list = new List<Art>();
                //LOOP THROUGH ROWS OF DATASET
                foreach (DataRow row in ds.Tables[0].Rows)
                {
                    list.Add(GenerateArtFromDataRow(row));
                }
                //RETURN LIST
                return list;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public IList<Art> GetDeletedArtByUserId(Int64? id)
        {
            try
            {
                //GET DATASET
                DataSet ds = Adapter.GetDeletedArtByUserId(id);
                //MAKE A LIST
                IList<Art> list = new List<Art>();
                //LOOP THROUGH ROWS OF DATASET
                foreach (DataRow row in ds.Tables[0].Rows)
                {
                    list.Add(GenerateArtFromDataRow(row));
                }
                //RETURN LIST
                return list;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }        
        public IList<Art> GetArtByCategoryId(Int64 id)
        {
            try
            {
                //GET DATASET
                DataSet ds = Adapter.GetArtByCategoryId(id);
                //MAKE A LIST
                IList<Art> list = new List<Art>();
                //LOOP THROUGH ROWS OF DATASET
                foreach (DataRow row in ds.Tables[0].Rows)
                {
                    list.Add(GenerateArtFromDataRow(row));
                }
                //RETURN LIST
                return list;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public IList<Art> GetArtForArtboardByUserId(Int64 id)
        {
            try
            {
                //GET DATASET
                DataSet ds = Adapter.GetArtForArtboardByUserId(id);
                //MAKE A LIST
                IList<Art> list = new List<Art>();
                //LOOP THROUGH ROWS OF DATASET
                foreach (DataRow row in ds.Tables[0].Rows)
                {
                    list.Add(GenerateArtFromDataRow(row));
                }
                //RETURN LIST
                return list;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }        
        #endregion

        #region INSERT METHODS
        [System.ComponentModel.DataObjectMethodAttribute
            (System.ComponentModel.DataObjectMethodType.Insert, true)]
        public int InsertArt(Art art)
        {
            try
            {
                return Adapter.InsertArt(art.Name, art.Description, art.Size, art.Price, art.Numberof, art.ModifiedDate, art.Spotlighted, art.ArtpictureId, art.UserId);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message, ex);
            }
        }
        #endregion

        #region UPDATE METHODS
        [System.ComponentModel.DataObjectMethodAttribute
            (System.ComponentModel.DataObjectMethodType.Insert, true)]
        public int UpdateArt(Art art)
        {
            try
            {
                return Adapter.UpdateArt(art.Id, art.Name, art.Description, art.Size, art.Price, art.Numberof, art.ModifiedDate, art.Spotlighted, art.ArtpictureId, art.UserId);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message, ex);
            }
        }
        [System.ComponentModel.DataObjectMethodAttribute
            (System.ComponentModel.DataObjectMethodType.Insert, true)]
        public int UpdateArtDeletedDate(Art art)
        {
            try
            {
                return Adapter.UpdateArtDeletedDate(art.Id, art.DeletedDate);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message, ex);
            }
        }        
        #endregion

        #region UTILITIES
        private Art GenerateArtFromDataRow(DataRow row)
        {
            Art obj = new Art();
            obj.Id = Convert.ToInt64(row["art_id"]);
            obj.Name = (string)row["art_name"];
            if (row["art_description"] != DBNull.Value)
            {
                obj.Description = (string)row["art_description"];
            }
            obj.Size = (string)row["art_size"];
            obj.Price = Convert.ToInt64(row["art_price"]);
            obj.Numberof = Convert.ToInt64(row["art_numberof"]);
            obj.CreationDate = (DateTime)row["art_creationdate"];
            if (row["art_modificationdate"] != DBNull.Value)
            {
                obj.ModifiedDate = (DateTime)row["art_modificationdate"];
            }
            if (row["art_spotlighted"] != DBNull.Value)
            {
                obj.Spotlighted = Convert.ToBoolean(row["art_spotlighted"]);
            }            
            obj.ArtpictureId = Convert.ToInt64(row["artpictures_artpicture_id"]);
            obj.UserId = Convert.ToInt64(row["users_user_id"]);            
            if (row.Table.Columns.Contains("category_name"))
            {
                obj.CategoryName = Convert.ToString(row["category_name"]);
            }
            if (row.Table.Columns.Contains("user_nickname"))
            {
                obj.UserNickname = Convert.ToString(row["user_nickname"]);
            }
            if (row.Table.Columns.Contains("user_id"))
            {
                obj.UserId = Convert.ToInt64(row["user_id"]);
            }
            if (row.Table.Columns.Contains("rating_stars"))
            {
                obj.RatingStars = Convert.ToString(row["rating_stars"]);
            }
            if (row.Table.Columns.Contains("category_name"))
            {
                obj.CategoryName = Convert.ToString(row["category_name"]);
            }
            //obj.UserId = Convert.ToInt64(row["users_user_id"]);
            /*if (row["art_deleteddate"] != DBNull.Value)
            {
                obj.DeletedDate = (DateTime)row["art_deleteddate"];
            }
            if (row["art_publisheddate"] != DBNull.Value)
            {
                obj.PublishedDate = (DateTime)row["art_publisheddate"];
            }*/
            return obj;
        }
        #endregion
    }
}
